<?php
	include "../database.php";
	
	$start_date = $_POST['start_date'];
	$finish_date =$_POST['finish_date'];
	$velocity = $_POST['velocity'];
	$curr_proj_id = $_POST['curr_proj_id'];
	
	
	$today = date("Y-m-d");
	$today = strtotime($today);
	$start_date_compare = strtotime($start_date);
	$finish_date_compare = strtotime($finish_date);
	
	$success = 0;
	
	// Poi��i �tevilo trenutnih sprintov tega projekta, da dolo�imo �tevilko sprinta
	$querySprint = "SELECT * FROM sprint WHERE project_id='$curr_proj_id';";
	$querySprintResult = executeQuery($querySprint);
	$sprints = mysql_num_rows($querySprintResult);
	
	
	//echo $start_date;
	//echo "<br/>";
	//$start_date = date('Y-m-d',strtotime($start_date));
	if ($velocity > 0) { 
		if($today <= $start_date_compare) {
			if($start_date_compare < $finish_date_compare){ 		
				
				$conflict = false;
				while ($row = mysql_fetch_array($querySprintResult)){
					$otherStartDate = strtotime($row["start_date"]);
					$otherFinishDate = strtotime($row["finish_date"]);
					
					if(($otherFinishDate >= $start_date_compare) && ($finish_date_compare >= $otherStartDate)) {
						$conflict = true;
					}
					
				}
				
				
				// Da dobimo �tevilko sprinta
				$sprintNumber = $sprints + 1;				
				
				
				// Vnesemo sprint v bazo
				if(!$conflict){ 
					$query = "INSERT INTO sprint (start_date, finish_date, velocity, project_id, sprint_number ) VALUES ('$start_date', '$finish_date', $velocity, $curr_proj_id, $sprintNumber);";
					executeQuery($query);
					
					
					
					//$msg_id = 200;
					$data = array('success' => '1');
					echo json_encode($data);
					
				// Conflict with other sprints!	
				} else {
					$msg_id=109;
					$data = array('msg' => message($msg_id) );
					echo json_encode($data);
				}
			// Start date must be before finish date!	
			} else {
				$msg_id=107;
				$data = array('msg' => message($msg_id), 'success' => $success );
				echo json_encode($data);
				
			}
		// Please pick date in the future!	
		} else {
			$msg_id=106;
			$data = array('msg' => message($msg_id), 'success' => $success );
			echo json_encode($data);
			
		}
	// Enter valid velocity number!	
	} else {
		$msg_id=108;
		$data = array('msg' => message($msg_id), 'success' => $success );
		echo json_encode($data);
		
	}
?>
